#include "import.h"
#include <iostream>
#include <fstream>
using namespace std; 

Customs::Customs()
{
    a = 0;
    b = 0;
    filesize = 0;
    SubChunk1Size = 0;
    AudioFormat = 0;
    NumChannels = 0;
    SampleRate = 0;
    ByteRate = 0;
    BlockAlign = 0;
    BitsPerSample = 0;
    BytesPerSample = 0;
    SubChunk2Size = 0; 
    strcpy(ChunkID, "RIFF");
    strcpy(Format, "WAVE");
    strcpy(SubChunk1ID, "fmt");
}

void Customs::Import(string filename)
{
  ifstream myfile (filename.c_str());
  if (myfile.is_open())
  {
    myfile.read((char*)header, 44);  // Reads the Full Header of the WAV File
    filesize = ((header[7]*256+header[6])*256+header[5])*256+header[4]+8; //FileSize is read like this due to being stored in Little Endian
    SubChunk1Size = ((header[19]*256+header[18])*256+header[17])*256+header[16]; //Follows Canonical WAVE Format
    AudioFormat = header[21]*256 + header[20]; 
    NumChannels = header[23]*256 + header[22];
    SampleRate = ((header[27]*256+header[26])*256+header[25])*256+header[24];
    ByteRate = ((header[31]*256+header[30])*256+header[29])*256+header[28];
    BlockAlign = header[33]*256+header[32];
    BitsPerSample = header[35]*256+header[34];
    BytesPerSample = BitsPerSample/8;
    SubChunk2Size = ((header[43]*256+header[42])*256+header[41])*256+header[40];
    DataDuration = SubChunk2Size/BytesPerSample/NumChannels;
    Data = new int * [NumChannels];
    
    for ( a= 0; a< NumChannels; a++)
    {
        Data[a] = new int [DataDuration];
        //Reminder to place Checker if memory was allocated
    }
    
    if (BytesPerSample == 1) 
    {
        for (int a = 0; a< DataDuration; a++)
        {
            for (int b = 0; b< NumChannels; b++)
            {
                myfile.read((char*)header, 1);
                Data[a][b] = (header[0] - 128)/128.0;
            } 
        }
    }
 
    else if (BytesPerSample == 2)
    {
        for (int a = 0; a<DataDuration; a++)
        {
            for(int b = 0; b< NumChannels; b++)
            {
                myfile.read((char*) header, 2);
                if(header[1] < 128)
                {
                    Data[a][b] = (header[1]*256+header[0])/32768.0;
                }
                else
                {
                    Data[a][b] = (header[1]*256+header[0]-65536)/32768.0;
                }
                
            }
        }
    }
   
    myfile.close();
  }

  else cout << "Unable to open file"; 
}

void Customs::Review()
{
    cout << "A: " << a << endl;
    cout << "B: " << b << endl;
    cout << "FileSize: " <<  filesize << endl;
    cout << "SubChink1Size: " << SubChunk1Size << endl;
    cout << "AudioFormat: " << AudioFormat << endl; 
    cout << "NumChannels: " << NumChannels << endl;
    cout << "SampleRate: " << SampleRate << endl;
    cout << "ByteRate: " << ByteRate << endl;
    cout << "BlockAlign: " << BlockAlign << endl;
    cout << "BitsPerSample: " << BitsPerSample << endl;
    cout << "BytesPerSample: " << BytesPerSample << endl;
    cout << "SubChunk2Size: " << SubChunk2Size << endl;
    cout << "DataDuration: " << DataDuration << endl;
    cout << ChunkID<< endl;
    cout << Format << endl;
    cout << SubChunk1ID << endl;
    for (int i = 0; i < 44; i++)
    {
        cout << "Header "<< i << ": " << header[i] << endl;
    }
}

/*
void Customs::Export(string filename, Customs file)
{
    ofstream myfile;
    
    myfile.open(filename.c_str());
    
    if (myfile.is_open())
    {
        myfile.write((char*)file.header, 0);
    }
    
}
*/
